package com.pafcio.dde.client;

import java.util.ArrayList;
import java.util.List;

import com.neva.DdeClientTransactionEvent;
import com.neva.DdeClientTransactionEventListener;
import com.pafcio.dde.shared.ConversationDetails;

public class CapturingDDEClient extends SimpleDDEClient implements Observer<DdeClientTransactionEvent>{

	private DdeClientTransactionEventListener transactionListener;
	private List<String> capturedData = new ArrayList<String>();

	public CapturingDDEClient withListener(CapturingListener transactionListener) {
		this.transactionListener = transactionListener;
		transactionListener.attach(this);
		return this;
	}
	
	@Override
	public CapturingDDEClient withConversationDetails(ConversationDetails conversationDetails) {
		super.withConversationDetails(conversationDetails);
		return this;
	}
	
	@Override
	public CapturingDDEClient withService(String service) {
		super.withService(service);
		return this;
	}
	
	@Override
	public CapturingDDEClient withTopic(String topic) {
		super.withTopic(topic);
		return this;
	}
	
	@Override
	protected void connect() throws Exception {
		super.connect();
		addDdeClientTransactionEventListener(transactionListener);
	}

	@Override
	public void update(DdeClientTransactionEvent event) {
		capturedData.add(String.valueOf(event.getDdeData()));
	}

	public List<String> getCapturedItems() {		
		return capturedData;
	}
}